import pathlib


class JavaClassAnalyzePreparer:
    def __init__(self,project_dir:pathlib.Path = pathlib.Path(__file__).parent.parent.parent.parent.parent.parent):
        self.project_dir:pathlib.Path = project_dir
        self.java_src_dir:pathlib.Path = self.get_java_src_dir()
        if not self.java_src_dir.exists():
            raise FileNotFoundError(f"Java source directory not found: {self.java_src_dir}")
        
    def get_java_src_dir(self):
        return self.project_dir/"src"/"main"/"java"
    
    def get_package_dir(self, package_name:str):
        package_dir = self.get_java_src_dir()
        for part in package_name.split('.'):
            package_dir /= part
            if not package_dir.exists():
                raise FileNotFoundError(f"Package directory not found: {package_dir}")
        return package_dir
    
    def get_classes(self,list_file:pathlib.Path=pathlib.Path(__file__).parent/"list.txt"):
        with open(list_file, "r") as f:
            class_list = [l.split("/") for l in f.readlines()]
        return class_list
    
    def verify_classes(self, class_list:list[list[str]]):
        main_package_name = "cn.edu.usst.cs"
        main_package_dir = self.get_package_dir(main_package_name)
        print(f"Main package directory: {main_package_dir}")
        if not main_package_dir.exists():
            raise FileNotFoundError(f"Main package directory not found: {main_package_dir}")
        for class_name_nodes in class_list:
            temp_dir= main_package_dir
            for class_name in class_name_nodes:
                temp_dir /= class_name
                if not temp_dir.exists():
                    raise FileNotFoundError(f"Class directory not found: {temp_dir}")
    

if __name__ == "__main__":
    preparer = JavaClassAnalyzePreparer()
    class_list = preparer.verify_classes(preparer.get_classes())
    print(class_list)